package com.liuyu.common.framework.generator.cf;

import com.framework.database.generator.*;
import com.google.common.collect.Sets;
import com.liuyu.common.framework.mybatis.plus.mapper.MybatisPlusBaseMapper;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName CodeGenerator
 * @Author: liuyu
 * @Date: Created in 2021/4/23 15:19
 * @Version: 1
 * @Modified By:
 */
public class CodeGenerator {
    public static void main(String[] args) {
        List<GenParam> paramList = new ArrayList<GenParam>();
        /**
         * 请保持生成参数的完整，不要注释
         */
        paramList.add(new GenParam("score",
//                "condition",
                new String[]{
                        "sc_project",
                        "sc_project_topic",
                        "sc_project_topic_item",
                        "sc_rule",
                        "sc_rule_item",
                        "sc_score",
                        "sc_score_item",
                        "sc_score_statistics",
                        "sc_score_statistics_detail",
                        "sc_user_score",
                        "sc_user_score_detail"
                }));
        GenConfig gc = new GenConfig();

        //通用baseMapper
        gc.setBaseMapperClass(MybatisPlusBaseMapper.class);
//        gc.setBaseMapperClassPath("com.baomidou.mybatisplus.core.mapper.BaseMapper");
        //是否需要注解
        gc.setClassAnnotation("");
        gc.setClassAnnotationPath(null);

        //sharejdbc 逻辑表
        gc.setRemoveTableSuffix("_0");
        //个性模板
        Map<GenType, String> map = new HashMap<>();
        map.put(GenType.DAO, "mybatis-plus/dao.ftlh");
        gc.setCustomTemplatePathMap(map);
        //个性pojo类统一后缀
        Map<GenType, String> suffixMap = new HashMap<>();
        suffixMap.put(GenType.DAO, "Mapper");
        suffixMap.put(GenType.PO, "");
        gc.setCustomerPojoSuffixMap(suffixMap);
        //个性上级路径
        Map<GenType, String> customPackage = new HashMap<>();
        customPackage.put(GenType.DAO, "mapper");
        gc.setCustomPathMap(customPackage);
//        customPackage.put(GenType.AO, "aoo");


        //设置乐观锁
        gc.setOptimisticField("version");
        gc.setOptimisticAnnotation("@Version");
        gc.setOptimisticAnnotationClass("com.baomidou.mybatisplus.annotation.Version");
        //设置时间支持jdk8 的时间。
        gc.setDateType(DateType.JDK8);


        gc.setBasePackage("com.liuyu.common.framework.mybatis");
        // 设置要忽略的表名前缀，默认空
        gc.setIgnoreTablePrefixes(new String[]{"prpl", "prpd"});
        // 设置PO是否保留前缀(设置忽略表名前缀时)，默认true
        gc.setKeepPrefixForPo(false);
        // 支持tkey特性
        gc.setTKeySupport(true);
        // 生成update语句时忽略的字段
        gc.setIgnoreColumnNamesWhenUpdate(new String[]{"tkey"});
        // 设置代码样式为DDD
        gc.setCodeStyle(CodeStyle.ALIBABA);
        // 设置 orm 类型，可选为mybatis,hibernate
        gc.setOrmType(OrmType.MYBATIS_PLUS);
        // 数据库相关配置
        // 设置基本保存目录（Java源代码根目录）
//        gc.setSaveDir("../score-core/src/main/java");
        gc.setSaveDir("score-dao/src/main/java");

        Map<GenType, String> customSaveDir = new HashMap<>();
        customSaveDir.put(GenType.AO, "score-common/src/main/java");
        customSaveDir.put(GenType.BO, "score-common/src/main/java");
        customSaveDir.put(GenType.DTO, "score-common/src/main/java");
        customSaveDir.put(GenType.QUERY, "score-common/src/main/java");
        customSaveDir.put(GenType.VO, "score-common/src/main/java");
        gc.setSaveDirMap(customSaveDir);
//        gc.setDbDriverName("org.postgresql.Driver");
        gc.setDbCatalog("score_cloud"); // MySQL的Catalog通常和数据库名一致，采用8.0+版驱动后需要设置
//        gc.setDbSchema("public");
        gc.setDbUrl("jdbc:mysql://56.101.1.33:3306/score_cloud?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8");
        gc.setDbUser("root");
        gc.setDbPassword("BD2kDD7hKbMNUmRg5Qac0EoXBKWbreRE1dqAPO4Z0FT/a4U9yCkhOTC7Yis6qEcYmO4zqkgHLCQi6RM77bi2jKZdR5cOxGpZAN9bsUyJnPeCkA4+FKPrt0CtFMT+ovb1uXgyfGz/i9Q=");
        // class支持生成的文件类型:生成PO、BASE_MAPPER_XML（自动覆盖）、Dao、VO、MapperXML（不覆盖）
        Set<GenType> genTypeSets = Sets.newLinkedHashSet();
        switch (gc.getCodeStyle()) {
            case DDD:
                //ddd 风格
//                gc.setGenTypes(Sets.newHashSet(GenType.PO, GenType.DAO, GenType.DTO, GenType.VO, GenType.BASE_MAPPER_XML, GenType.MAPPER_XML));
                genTypeSets.add(GenType.DO);
                genTypeSets.add(GenType.DAO);
                genTypeSets.add(GenType.DTO);
                genTypeSets.add(GenType.VO);
                genTypeSets.add(GenType.BASE_MAPPER_XML);
                genTypeSets.add(GenType.MAPPER_XML);
                break;
            case ALIBABA:
                //阿里风格
//                gc.setGenTypes(Sets.newHashSet(GenType.DO, GenType.DAO, GenType.DTO, GenType.VO, GenType.BO, GenType.QUERY, GenType.AO, GenType.BASE_MAPPER_XML, GenType.MAPPER_XML));
                genTypeSets.add(GenType.DO);
                genTypeSets.add(GenType.DAO);
                genTypeSets.add(GenType.DTO);
                genTypeSets.add(GenType.VO);
                genTypeSets.add(GenType.BO);
                genTypeSets.add(GenType.QUERY);
                genTypeSets.add(GenType.AO);
                genTypeSets.add(GenType.BASE_MAPPER_XML);
                genTypeSets.add(GenType.MAPPER_XML);
                gc.setGenTypes(genTypeSets);
                break;
            default:
//                gc.setGenTypes(Sets.newHashSet(GenType.PO, GenType.DAO, GenType.VO, GenType.BASE_MAPPER_XML, GenType.MAPPER_XML));
                genTypeSets.add(GenType.DO);
                genTypeSets.add(GenType.DAO);
                genTypeSets.add(GenType.VO);
                genTypeSets.add(GenType.BASE_MAPPER_XML);
                genTypeSets.add(GenType.MAPPER_XML);
                break;
        }
        //是否允许覆盖vo，dto,bo,ao,query
        gc.setFileOverride(true);
        //如果有个性配置，则走个性配置
        Map<GenType, Boolean> fileOverride = new HashMap<>();
        fileOverride.put(GenType.QUERY, false);
//        fileOverride.put(GenType.AO, true);
        gc.setFileOverrideMap(fileOverride);
        //class 注解
        GenComment comment = new GenComment();
        //作者
        comment.setAuthor("liuyu");
        //创建时间
        comment.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        //版本
        comment.setVersion(1);
        //版权
        comment.setCopyright("");
        //vo,ao,do 类的相关解释
        Map<GenType, String> useDescriptionMap = new HashMap<>();
//        PO->DO->DTO->VO
        //https://blog.csdn.net/qq_41720396/article/details/124825897
        useDescriptionMap.put(GenType.DO, "Service层,Manager层,DAO层，最底层放到dao层");
        useDescriptionMap.put(GenType.QUERY, "Query:数据查询对象，各层接收上层的查询请求。注意超过 2 个参数的查询封装，禁止使用 Map 类来传输。放到最底层 dao层");
        useDescriptionMap.put(GenType.VO, "VO(View Object):显示层对象，通常是 Web 向模板渲染引擎层传输的对象。DTO转VO,使用场景，在接口层服务中，将DTO转成VO,返回给前台,Controller层，放到最顶层web层");
        useDescriptionMap.put(GenType.BO, "业务对象。可以由 Service 层输出的封装业务逻辑的对象。主要在服务内部使用的业务对象,使用场景，在服务层服务中，由DTO转成BO然后进行业务处理后，转成DTO返回到接口层 Service层、Manager层，放到service层api模块");
        useDescriptionMap.put(GenType.DTO, "DTO(Data Transfer Object):数据传输对象，Service 或 Manager 向外传输的对象。DO转DTO Service层、Manager层，放到service层api模块");
        useDescriptionMap.put(GenType.AO, "AO(ApplicationObject):应用对象，在Web层与Service层之间抽象的复用对象模型， 极为贴近展示层，复用度不高。放到service层的api模块");
        comment.setUseDescriptionMap(useDescriptionMap);


        gc.setComment(comment);

        //获取编译依赖,解决编译的问题。
        List<String> dependencies = new ArrayList<>();
        dependencies.add("F:\\repository\\org\\projectlombok\\lombok\\1.18.26\\lombok-1.18.26.jar");
        dependencies.add("F:\\repository\\com\\fasterxml\\jackson\\core\\jackson-annotations\\2.13.5\\jackson-annotations-2.13.5.jar");
        dependencies.add("F:\\repository\\io\\swagger\\core\\v3\\swagger-annotations\\2.2.10\\swagger-annotations-2.2.10.jar");
        dependencies.add("F:\\repository\\com\\baomidou\\mybatis-plus-annotation\\3.5.2\\mybatis-plus-annotation-3.5.2.jar");
        dependencies.add("F:\\repository\\io\\swagger\\swagger-annotations\\1.5.22\\swagger-annotations-1.5.22.jar");
        gc.setDependentJarUrls(dependencies);

        Generator generator = new Generator();
        generator.setGenConfig(gc);
        generator.setParamList(paramList);
        generator.help();
        generator.generate();
    }
}